# 环境同步工具 - 将FormTest的改动同步到Form1
# 作者: 系统自动生成
# 用途: 将开发环境(FormTest)的改动同步到生产环境(Form1)

param(
    [switch]$Backup = $true,        # 是否备份原文件
    [switch]$Selective = $false,    # 是否选择性同步
    [switch]$DryRun = $false,       # 是否只预览不执行
    [switch]$Force = $false         # 是否强制覆盖
)

# 获取脚本所在目录
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$ProjectDir = $ScriptDir

# 定义文件映射关系
$FileMappings = @{
    "FormTest.cs" = "Form1.cs"
    "FormTest.Designer.cs" = "Form1.Designer.cs" 
    "FormTest.resx" = "Form1.resx"
}

# 定义需要替换的内容映射
$ContentMappings = @{
    "FormTest" = "Form1"
    "测试环境" = ""
    "\[测试环境\]" = ""
}

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "环境同步工具 - FormTest -> Form1" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan

# 检查源文件是否存在
$MissingFiles = @()
foreach ($SourceFile in $FileMappings.Keys) {
    $SourcePath = Join-Path $ProjectDir $SourceFile
    if (-not (Test-Path $SourcePath)) {
        $MissingFiles += $SourceFile
    }
}

if ($MissingFiles.Count -gt 0) {
    Write-Host "错误: 以下源文件不存在:" -ForegroundColor Red
    $MissingFiles | ForEach-Object { Write-Host "  - $_" -ForegroundColor Red }
    exit 1
}

# 创建备份目录
if ($Backup) {
    $BackupDir = Join-Path $ProjectDir "Backup_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
    if (-not $DryRun) {
        New-Item -ItemType Directory -Path $BackupDir -Force | Out-Null
        Write-Host "备份目录: $BackupDir" -ForegroundColor Green
    } else {
        Write-Host "[预览] 将创建备份目录: $BackupDir" -ForegroundColor Yellow
    }
}

# 同步文件
foreach ($SourceFile in $FileMappings.Keys) {
    $TargetFile = $FileMappings[$SourceFile]
    $SourcePath = Join-Path $ProjectDir $SourceFile
    $TargetPath = Join-Path $ProjectDir $TargetFile
    
    Write-Host "`n处理文件: $SourceFile -> $TargetFile" -ForegroundColor White
    
    # 检查目标文件是否存在
    if ((Test-Path $TargetPath) -and -not $Force -and -not $DryRun) {
        if ($Selective) {
            $Response = Read-Host "目标文件 $TargetFile 已存在，是否覆盖? (y/N)"
            if ($Response -ne 'y' -and $Response -ne 'Y') {
                Write-Host "  跳过文件: $TargetFile" -ForegroundColor Yellow
                continue
            }
        }
    }
    
    # 备份原文件
    if ($Backup -and (Test-Path $TargetPath)) {
        $BackupPath = Join-Path $BackupDir $TargetFile
        if (-not $DryRun) {
            Copy-Item $TargetPath $BackupPath -Force
            Write-Host "  已备份: $TargetFile" -ForegroundColor Green
        } else {
            Write-Host "  [预览] 将备份: $TargetFile" -ForegroundColor Yellow
        }
    }
    
    # 读取源文件内容
    $Content = Get-Content $SourcePath -Raw -Encoding UTF8
    
    # 应用内容映射替换
    foreach ($Find in $ContentMappings.Keys) {
        $Replace = $ContentMappings[$Find]
        $Content = $Content -replace $Find, $Replace
    }
    
    # 写入目标文件
    if (-not $DryRun) {
        $Content | Set-Content $TargetPath -Encoding UTF8
        Write-Host "  已同步: $SourceFile -> $TargetFile" -ForegroundColor Green
    } else {
        Write-Host "  [预览] 将同步: $SourceFile -> $TargetFile" -ForegroundColor Yellow
    }
}

Write-Host "`n========================================" -ForegroundColor Cyan
if ($DryRun) {
    Write-Host "预览模式完成 - 未实际修改任何文件" -ForegroundColor Yellow
    Write-Host "要执行实际同步，请移除 -DryRun 参数" -ForegroundColor Yellow
} else {
    Write-Host "环境同步完成!" -ForegroundColor Green
    Write-Host "建议执行以下操作:" -ForegroundColor White
    Write-Host "1. 编译项目验证同步结果" -ForegroundColor White
    Write-Host "2. 测试Form1功能是否正常" -ForegroundColor White
    Write-Host "3. 如有问题可从备份目录恢复" -ForegroundColor White
}
Write-Host "========================================" -ForegroundColor Cyan

# 显示使用帮助
if ($args.Count -eq 0 -and -not $DryRun) {
    Write-Host "`n使用帮助:" -ForegroundColor Cyan
    Write-Host "  .\SyncEnvironment.ps1                    # 默认同步（带备份）" -ForegroundColor White
    Write-Host "  .\SyncEnvironment.ps1 -DryRun           # 预览模式" -ForegroundColor White
    Write-Host "  .\SyncEnvironment.ps1 -Selective        # 选择性同步" -ForegroundColor White
    Write-Host "  .\SyncEnvironment.ps1 -Force            # 强制覆盖" -ForegroundColor White
    Write-Host "  .\SyncEnvironment.ps1 -Backup:`$false    # 不备份" -ForegroundColor White
}